<template>
	<view class="code">
		<view class="">
			<u-navbar :autoBack="true"></u-navbar>
		</view>
		<view class="code-main">
			<view class="code-msg">
				请输入6位验证码
			</view>
			<view class="code-time">
				<view class="">
					验证码已发送至 {{setPhone}}
				</view>
				<view class="" v-if="isShow">
					{{codeNum}}s后重发
				</view>
				<view class="" v-if="!isShow" @click="sendCode">
					收不到验证码?
				</view>
			</view>
			<view class="code-num">
				<u-code-input v-model="value3" :maxlength="6"></u-code-input>
			</view>
		</view>
	</view>
</template>

<script>
	import {sendRegisterOrLoginCaptcha,loginByMobile} from '@/api/login.js'
	import {mapMutations} from 'vuex'
	export default {
		data() {
			return {
				phone:'',
				codeNum: 10,
				isShow: true
			}
		},
		onLoad(e) {
			this.phone = e.tel
			this.sendCode()
			// sendRegisterOrLoginCaptcha({
			// 	mobile: this.phone
			// }).then(res=>{
			// 	console.log(res)
			// })
		},
		methods: {
			...mapMutations(['setToken']),
			sendCode(){
				this.isShow= true
				let timer = setInterval(()=>{
					this.codeNum--
					if(this.codeNum <= 0){
						this.codeNum = 10
						this.isShow = false
						clearInterval(timer)
					}
				},1000)
			}
		},
		computed:{
			setPhone(){
				let arr = this.phone.split('')
				arr.splice(3,0,' ')
				arr.splice(8,0,' ')
				console.log(arr.join(''))
				return arr.join('')
			}
		},
		watch:{
			valCode(newVal, oldVal){
				if(newVal.length == 6){
					//发起请求
					loginByMobile({
						mobile: this.phone,
						captcha: newVal
					}).then(res=>{
						console.log(res)
						if(res.meta.code=='10086'){
							this.setToken(res.accessToken)
							//返回 我的 页面
							uni.switchTab({
								url:'/pages/tabs/me'
							})
						}
					})
				}
			}
		}
	}
</script>

<style>
	.code {
		width: 100vw;
		height: 100vh;
		background: rgba(255, 255, 255, 0.39);
	}
	.code-main{
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		padding-top: 163rpx;
		padding-left: 62rpx;
		padding-right: 62rpx;
	}
	.code-msg{
		font-size: 56rpx;
		font-weight: bold;
		color: #343234;
	}
	.code-time{
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		padding-top: 33rpx;
		font-size: 28rpx;
		font-weight: 400;
		color: #343234;
		letter-spacing: 2rpx;
		opacity: 0.53;
	}
	.code-num{
		display: flex;
		justify-content: center;
		width: 100%;
		padding-top: 83rpx;
	}
</style>